package com.aplana.ccsd.web.template;

import info.magnolia.module.blossom.annotation.Area;
import info.magnolia.module.blossom.annotation.TabFactory;
import info.magnolia.module.blossom.annotation.Template;
import info.magnolia.module.blossom.dialog.TabBuilder;

import java.util.List;

import javax.jcr.Node;
import javax.jcr.RepositoryException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.aplana.ccsd.model.Certificate;
import com.aplana.ccsd.model.MenuItem;
import com.aplana.ccsd.service.CertificateService;
import com.aplana.ccsd.service.NavigationMenuService;

/**
 * Template for certificate pages.
 */
@Controller
@Template(title = "Aplana Certificate", id = "blossomCCSD:pages/certificate")
public class CertificateTemplate {
	
	@SuppressWarnings("unused")
	private static final Logger LOG = LoggerFactory
			.getLogger(CertificateTemplate.class);
	
	@Autowired
    private NavigationMenuService navigationMenuService;
	
	
	@Area("mainInfo")
	@Controller
	public static class MainInfoArea {
		
		@Autowired
		CertificateService certificateService;

	    @TabFactory("Content")
	    public void contentTab(TabBuilder tab) {
	    	certificateService.addCertificateDialogControls(tab);
	    }

	    @RequestMapping("/certificateTemplate/mainInfo")
	    public String render(Node page, ModelMap model) throws RepositoryException {
	    	Certificate item = certificateService.getCertificate(page);
	    	model.put("certificate", item);
	        return "areas/certificate.jsp";
	    }
	}
	
	@Area("vertical")
	@Controller
	public static class VerticalArea {

	    @TabFactory("Content")
	    public void contentTab(TabBuilder tab) {
	    }

	    @RequestMapping("/certificateTemplate/vertical")
	    public String render(Node page, ModelMap model) {
	        return "areas/verticalArea.jsp";
	    }
	}

    @RequestMapping("/certificateTemplate")
    public String render(Node page, ModelMap model) throws RepositoryException {
        List<MenuItem> topMenus = navigationMenuService.getTopMenus(page);
        model.put("topMenus", topMenus);

        return "pages/objectInfo.jsp";
    }

    @TabFactory("Content")
    public void propertiesDialog(TabBuilder tab) {
        tab.addEdit("title", "Title", "");
        tab.addCheckbox("hideInNavigation", "Hide in navigation", "Check this box to hide this page in navigation");
    }
}
